﻿using System;
using System.Data;

namespace VOL.WebApi.VOL.WebApi.Operator.Wms
{
    public class T_VIEW_RMOperator
    {
        public static bool SynchronizeSuppliersOperator()
        {
            string sql;
            try
            {

                DateTime datetime = GetSynchronizeSuppliersDateStamp();


                sql = @" SELECT T_RM_ARE, T_RM_ARE_CODE, T_RM_PROVINCE, T_RM_PROVINCE_CODE, T_RM_CITY, 
                T_RM_CITY_CODE, T_RM_NAME, T_RM_CODE, T_RM_TYPE, T_RM_FL, T_RM_FL_CODE, 
                T_RM_COMPANY_NATURE_CODE, T_RM_COMPANY_NATURE, T_RM_BUSINESS_START_DATE, 
                T_RM_BUSINESS_START_END, T_RM_WORK_STATUE, T_RM_WORK_STATUE_CODE, T_RM_COR_REP, 
                T_RM_REGISTERED_CAPITAL, T_RM_TAXPAYER_QUALIFICATION, T_RM_TAXPAYER_QUALIFICATION_CODE, 
                T_RM_OFFICE_ADD, T_RM_BUSINESS, T_RM_REGISTERED_ADD, T_RM_UNI_CODE, T_RM_BANK_DEPOSIT, 
                T_RM_BANK_DEPOSIT_CODE, T_RM_BANK_NAME, T_RM_BANK_ACCOUNT, T_RM_WEBSITE, T_RM_OFFICE_TEL, 
                T_RM_ZIP_CODE, T_RM_FAX, T_RM_REMARK, EXP2, EXP3, T_CREATE_DATE, T_UPDATE_DATE
            FROM [DASContract].[dbo].[VIEW_RM] where T_CREATE_DATE>='" + datetime + "'";
                DataTable dt = new DataTable();
                dt = PurchasePub.SqlQuery(sql);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (!IsExistSupplier(dt.Rows[i]["T_RM_UNI_CODE"].ToString()))
                        {
                            sql = @"insert into T_VIEW_RM(guid, T_RM_ARE, T_RM_ARE_CODE, T_RM_PROVINCE, T_RM_PROVINCE_CODE, T_RM_CITY, 
                T_RM_CITY_CODE, T_RM_NAME, T_RM_CODE, T_RM_TYPE, T_RM_FL, T_RM_FL_CODE, 
                T_RM_COMPANY_NATURE_CODE, T_RM_COMPANY_NATURE, T_RM_BUSINESS_START_DATE, 
                T_RM_BUSINESS_START_END, T_RM_WORK_STATUE, T_RM_WORK_STATUE_CODE, T_RM_COR_REP, 
                T_RM_REGISTERED_CAPITAL, T_RM_TAXPAYER_QUALIFICATION, T_RM_TAXPAYER_QUALIFICATION_CODE, 
                T_RM_OFFICE_ADD, T_RM_BUSINESS, T_RM_REGISTERED_ADD, T_RM_UNI_CODE, T_RM_BANK_DEPOSIT, 
                T_RM_BANK_DEPOSIT_CODE, T_RM_BANK_NAME, T_RM_BANK_ACCOUNT, T_RM_WEBSITE, T_RM_OFFICE_TEL, 
                T_RM_ZIP_CODE, T_RM_FAX, T_RM_REMARK, EXP2, EXP3, T_CREATE_DATE, T_UPDATE_DATE )";
                            string guid = Guid.NewGuid().ToString();
                            sql += " values('" + guid + "','" + dt.Rows[i]["T_RM_ARE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_ARE_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_PROVINCE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_PROVINCE_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_CITY"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RM_CITY_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_TYPE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_FL"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_FL_CODE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RM_COMPANY_NATURE_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_COMPANY_NATURE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_BUSINESS_START_DATE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RM_BUSINESS_START_END"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_WORK_STATUE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_WORK_STATUE_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_COR_REP"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RM_REGISTERED_CAPITAL"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_TAXPAYER_QUALIFICATION"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_TAXPAYER_QUALIFICATION_CODE"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RM_OFFICE_ADD"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_BUSINESS"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_REGISTERED_ADD"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_UNI_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_BANK_DEPOSIT"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RM_BANK_DEPOSIT_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_BANK_NAME"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_BANK_ACCOUNT"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_WEBSITE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_OFFICE_TEL"].ToString() + "',";

                            sql += "'" + dt.Rows[i]["T_RM_ZIP_CODE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_FAX"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_RM_REMARK"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["EXP2"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["EXP3"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_CREATE_DATE"].ToString() + "',";
                            sql += "'" + dt.Rows[i]["T_UPDATE_DATE"].ToString() + "')";
                            WmsPub.SqlExecute(sql);
                        }

                    }
                }
                return true;
            }
            catch

            {
                return false;
            }


        }
        /// <summary>
        /// 根据统一代码查是否存在
        /// </summary>
        /// <param name="T_RM_UNI_CODE"></param>
        /// <returns>false不存在，true存在</returns>
        private static bool IsExistSupplier(string T_RM_UNI_CODE)
        {
            if (T_RM_UNI_CODE != "")
            {
                return false;
            }
            else
            {
                string sql;
                sql = "select T_RM_UNI_CODE from [T_VIEW_RM] where T_RM_UNI_CODE='" + T_RM_UNI_CODE + "'  ";
                DataTable tb = WmsPub.SqlQuery(sql);
                if (tb != null)
                {
                    if (tb.Rows.Count > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    return false;
                }
            }


        }

        /// <summary>
        /// 获得供应商同步的最大时间戳
        /// </summary>
        /// <returns></returns>
        private static DateTime GetSynchronizeSuppliersDateStamp()
        {
            string sql;
            sql = "select  isnull(max(T_CREATE_DATE),'') from [T_VIEW_RM]   ";
            DataTable tb = WmsPub.SqlQuery(sql);
            if (tb != null)
            {
                if (tb.Rows[0][0].ToString() != "")
                {
                    return DateTime.Parse(tb.Rows[0][0].ToString());
                }
                else
                {
                    return DateTime.Parse("2001-01-01");
                }

            }
            else
            {
                return DateTime.Parse("2001-01-01");
            }


        }
    }
}
